<template>
    <div class="app-container">

        <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
            <el-table-column class-name="status-col" label="发送人" width="180" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.sender_name }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="收信人" width="180" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.receiver_name }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="内容" width="330" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.content }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="是否已读" width="110" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.is_read == 0 ? "未读" : "已读" }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="发送时间" width="220" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ dayjs(scope.row.create_time).format("YYYY-MM-DD HH:mm:ss") }}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="doRead(scope.row)">已读</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 50, 100]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

    </div>
</template>
<script>
import api from "@/api/msg/msg";
import dayjs from "dayjs";

export default {
    name: "msg-msg-list",
    data() {
        return {
            list: null,
            listLoading: false,
            page: 1,
            limit: 10,
            total: 0,
            ids: [],
            addV: false,
            addForm: {},
            editV: false,
            editForm: {},
            dayjs: dayjs,
        }
    },
    created() {
        this.initialize();

    },
    methods: {

        handleSizeChange(val) {
            this.page = 1;
            this.limit = val;
            this.initialize();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.initialize();
        },
        async initialize() {
            let sdata = {
                page: this.page,
                limit: this.limit
            }
            let res = await api.listOwnMsg(sdata);
            this.total = res.count;
            this.list = [...res.data];
        },

        async doRead(val) {
            let sdata = {
                id: val.id,
                sender: val.sender,
                receiver: val.receiver,
                content: val.content,
                create_time: val.create_time,
                is_read: 1
            };
            let res = await api.edit(sdata);
            if (res.code == 0) {
                this.$message.success(res.msg);
                this.initialize();
                this.editV = false;
            }
        }
    }
}
</script>